package mq.demo.pubsub;

import cn.itlaobing.Util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author mifei
 * @create 2018年04月12日
 **/
public class Send {

    private final static String EXCHANGE_NAME = "exchange_fanout";

    public static void main(String[] argv) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        for (int i = 0; i < 50; i++) {

            // 消息内容
            String message = "Hello world"+i;
            // 与前面不同, 生产者将消息发送给exchange, 而非队列. 若发消息时还没消费者绑定queue与该exchange, 消息将丢失
            channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        }
        channel.close();
        connection.close();
    }
}
